package com.zhongzhi.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.zhongzhi.springboot.pojo.Equipment;
import com.zhongzhi.springboot.pojo.EquipmentShow;
/**
 * 此对象为plc数据获取类
 * @author Administrator
 *
 */
public class PlcUtil {

	/**获取plc连接套接字对象*/
	public  Socket getSocket(EquipmentShow e) throws IOException {
		Integer port = e.getPort();
		String host = e.getHost();
		if(host!=null && port !=null) {
			Socket socket = new Socket(host,port);
			return socket;
		}else {
			return null;
		}
	}
	
	/**发送数据返回字符串*/
	public String getString(String s,InputStream inputStream,OutputStream outputStream) throws IOException {
			outputStream.write(s.getBytes());
			outputStream.flush();
			int len = inputStream.read();
			char[] inputchar = new char[511];
			for (int i = 0; i < 511; i++) {
				inputchar[i] = (char)(byte)inputStream.read();
			}
			return String.copyValueOf(inputchar);
		}
		
		
	
	
	/**将获取的数据信息分解,获取通道数据*/
	public Map<Integer,Double> getData(String s) {
		 String[] split = s.split(",");
		 Map<Integer,Double> map = new HashMap<Integer, Double>();
		 for (int i = 0; i < split.length; i++) {
			 try {
				 Double d = Double.valueOf(split[i].trim());
//				 System.out.println(d);
				 map.put(i+1, d);
			} catch (Exception e) {
				System.out.println("字符串解析为浮点数出错");
			}
			 
		}
		return map;
	}


	
}
